package com.example.idachuappone.person.item;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import java.util.HashMap;
import java.util.Map;

public class IdeaActivity extends BaseActivity implements OnClickListener {
    private Button btn_back;
    private Button btn_index;
    private Dialog dialog;
    private EditText et_content;
    private EditText et_phone;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idea);
        this.dialog = ComUtil.createCusProgressSmallBlackBgDialog(this);
        initView();
    }

    private void initView() {
        this.btn_back = (Button) findViewById(R.id.btn_back);
        this.btn_back.setOnClickListener(this);
        this.btn_index = (Button) findViewById(R.id.btn_index);
        this.btn_index.setOnClickListener(this);
        this.et_content = (EditText) findViewById(R.id.et_content);
        this.et_content.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
                if (IdeaActivity.this.et_content.getText().toString().trim().length() <= 0 || IdeaActivity.this.et_phone.getText().toString().trim().length() <= 0) {
                    IdeaActivity.this.btn_index.setEnabled(false);
                    IdeaActivity.this.btn_index.setTextColor(Color.parseColor("#e6e6e6"));
                    return;
                }
                IdeaActivity.this.btn_index.setEnabled(true);
                IdeaActivity.this.btn_index.setTextColor(IdeaActivity.this.getResources().getColor(R.color.color_two_main));
            }
        });
        this.et_phone = (EditText) findViewById(R.id.et_text);
        this.et_phone.addTextChangedListener(new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void afterTextChanged(Editable s) {
                if (IdeaActivity.this.et_content.getText().toString().trim().length() <= 0 || IdeaActivity.this.et_phone.getText().toString().trim().length() <= 0) {
                    IdeaActivity.this.btn_index.setEnabled(false);
                    IdeaActivity.this.btn_index.setTextColor(Color.parseColor("#e6e6e6"));
                    return;
                }
                IdeaActivity.this.btn_index.setEnabled(true);
                IdeaActivity.this.btn_index.setTextColor(IdeaActivity.this.getResources().getColor(R.color.color_two_main));
            }
        });
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back /*2131165198*/:
                finish();
            case R.id.btn_index /*2131165213*/:
                String content = this.et_content.getText().toString();
                String phone = this.et_phone.getText().toString();
                if (content == null || content.trim().length() <= 0) {
                    MainToast.show(this, "\u4eb2\uff0c\u8bf7\u586b\u5199\u60a8\u7684\u5efa\u8bae", 0);
                } else if (phone == null || phone.trim().length() <= 0) {
                    MainToast.show(this, "\u4eb2\uff0c\u8bf7\u586b\u5199\u60a8\u7684\u624b\u673a\u53f7\u6216\u90ae\u7bb1", 0);
                } else if (CheckUtil.isCellphone(phone) || CheckUtil.isEmail(phone)) {
                    submitIdea(content, phone);
                } else {
                    MainToast.show(this, "\u4eb2\uff0c\u60a8\u586b\u5199\u7684\u624b\u673a\u53f7\u6216\u90ae\u7bb1\u683c\u5f0f\u6709\u8bef", 0);
                }
            default:
        }
    }

    private void submitIdea(String content, String phone) {
        if (CheckUtil.checkNet(this)) {
            Map<String, String> map = new HashMap();
            map.put("content", content);
            map.put("phone", phone);
            NetUtil.post(this, Constant.SUBMITIDEA, map, new RequestCallBack<String>() {
                public void onStart() {
                    super.onStart();
                    IdeaActivity.this.dialog.show();
                }

                public void onSuccess(ResponseInfo<String> responseInfo) {
                    IdeaActivity.this.dialog.dismiss();
                    MainToast.show(IdeaActivity.this, "\u610f\u89c1\u5df2\u6210\u529f\u4e0a\u4f20", 1);
                    IdeaActivity.this.finish();
                }

                public void onFailure(HttpException arg0, String arg1) {
                    NetUtil.netFail(IdeaActivity.this, IdeaActivity.this.dialog, true);
                }
            });
            return;
        }
        MainToast.show(this, getResources().getString(R.string.txt_network), 0);
    }
}
